import { createRouter, createWebHistory } from "vue-router";
import ProductDet from "@/views/BuyerL/ProductDet.vue";
import HomeView from "../views/AdminG/HomePage.vue";
// 引入页面组件
import AddProductPage from "../views/AdminY/AddProduct.vue";
import EditProductPage from "../views/AdminY/EditProduct.vue";
import ProductDetailsPage from "../views/AdminY/ProductDetail.vue";
import ProductList from "../views/AdminY/ProductList.vue";
import OrderList from "../views/AdminY/OrderList.vue";
import OrderDetails from "../views/AdminY/OrderDetails.vue";
import UserLocation from "../views/BuyerL/UserLocation.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: "/", redirect: "/login" }, // 修正重定向路径，指向登录路由名称
    //L-嵌套路由
    {
      path: "/bmanager",
      meta: { requiresAuth: true, role: "BUYER" },
      component: () => import("../views/BuyerL/BManager.vue"),
      children: [
        {
          path: "index",
          name: "index",
          meta: { title: "商城首页", requiresAuth: true, role: "BUYER" },
          component: () => import("../views/BuyerL/Index.vue"),
          children: [
            {
              path: "products",
              name: "products",
              meta: { requiresAuth: true, role: "BUYER" },
              component: () => import("../views/BuyerL/Products.vue"),
            },
            {
              path: "shoppingcart",
              name: "shoppingcart",
              meta: { requiresAuth: true, role: "BUYER" },
              component: () => import("../views/BuyerL/ShoppingCart.vue"),
            },
            {
              path: "collection",
              name: "collection",
              meta: { requiresAuth: true, role: "BUYER" },
              component: () => import("../views/BuyerL/Collection.vue"),
            },
            {
              path: "userdet",
              name: "UserDet",
              meta: { title: "个人信息", requiresAuth: true, role: "BUYER" },
              component: () => import("../views/BuyerL/UserDet.vue"),
            },
            {
              path: "userorders",
              name: "UserOrders",
              meta: { title: "我的商城", requiresAuth: true, role: "BUYER" },
              component: () => import("../views/BuyerL/UserOrders.vue"),
            },
            {
              path: "myorder",
              name: "MyOrder",
              meta: { title: "我的订单", requiresAuth: true, role: "BUYER" },
              component: () => import("../views/BuyerL/MyOrder.vue"),
            },
            {
              path: "userlocation",
              name: "UserLocation",
              meta: {
                title: "用户地址管理",
                requiresAuth: true,
                role: "BUYER",
              },
              component: () => import("../views/BuyerL/UserLocation.vue"),
            },
            {
              path: "/payment",
              name: "Payment",
              meta: { title: "付款详情", requiresAuth: true, role: "BUYER" },
              component: () => import("../views/BuyerL/Payment.vue"),
            },
          ],
        },
        {
          path: "/product/:productId",
          name: "productDetails",
          meta: { title: "商品详情", requiresAuth: true, role: "BUYER" },
          component: () => import("../views/BuyerL/ProductDet.vue"),
        },
      ],
    },

    //G-路由嵌套
    {
      path: "/manager",
      meta: { requiresAuth: true, role: "ADMIN" },
      component: import("../views/AdminG/Manager.vue"),
      children: [
        {
          path: "home", //不要写斜杠哦哦哦哦哦！！！！
          name: "home",
          meta: { title: "主页", requiresAuth: true, role: "ADMIN" },
          component: () => import("../views/AdminG/HomePage.vue"), //manager/home访问
        },

        {
          path: "password",
          name: "password",
          meta: { title: "修改密码", requiresAuth: true, role: "ADMIN" },
          component: () => import("../views/AdminG/Password.vue"),
        },
        {
          path: "publicProduct",
          name: "publicProduct",
          meta: { title: "发布商品", requiresAuth: true, role: "ADMIN" },
          component: () => import("../views/AdminG/product/PublicProduct.vue"),
        },
        {
          path: "buyer",
          name: "buyer",
          meta: { title: "买家信息", requiresAuth: true, role: "ADMIN" },
          component: () => import("../views/AdminG/buyer/Buyer.vue"),
        },

        {
          path: "homepage",
          name: "homepage",
          meta: { title: "首页", requiresAuth: true, role: "ADMIN" },
          component: () => import("../views/AdminG/HomePage.vue"),
        },
        //Y-路由嵌套
        {
          path: "products",
          name: "ProductList",
          meta: { title: "商品列表", requiresAuth: true, role: "ADMIN" },
          component: () => import("../views/AdminY/ProductList.vue"), // 商品列表
        },
        {
          path: "services",
          name: "SalesServices",
          meta: { title: "售后服务", requiresAuth: true, role: "ADMIN" },
          component: () => import("../views/AdminY/SalesServices.vue"), // 订单列表
        },
        {
          path: "orders",
          name: "OrderList",
          meta: { title: "订单列表", requiresAuth: true, role: "ADMIN" },
          component: () => import("../views/AdminY/OrderList.vue"), // 订单列表
        },
        {
          path: "logistics",
          name: "LogisticsManagement",
          meta: { title: "物流管理", requiresAuth: true, role: "ADMIN" },
          component: () => import("../views/AdminY/LogisticsManagement.vue"), // 订单列表
        },
        {
          path: "merchant-apply",
          name: "MerchantApply",
          meta: { title: "商户认证申请", requiresAuth: true, role: "ADMIN" },
          component: () => import("../views/AdminY/MerchantApply.vue"),  // 路径更新
        },
        {
          path: "/orders/:id",
          name: "OrderDetails",
          meta: { title: "订单详情", requiresAuth: true, role: "ADMIN" },
          component: () => import("../views/AdminY/OrderDetails.vue"), // 订单详情
          props: true, // 启用 props 接收 id 参数
        },
        {
          path: "/products/add",
          name: "AddProduct",
          meta: { title: "添加商品", requiresAuth: true, role: "ADMIN" },
          component: () => import("../views/AdminY/AddProduct.vue"), // 添加商品
        },
        {
          path: "/products/edit/:id",
          name: "EditProduct",
          meta: { title: "编辑商品", requiresAuth: true, role: "ADMIN" },
          component: () => import("../views/AdminY/EditProduct.vue"), // 编辑商品
          props: true, // 启用 props 接收 id 参数
        },
        {
          path: "/products/:id",
          name: "ProductDetails",
          meta: { title: "商品详情", requiresAuth: true, role: "ADMIN" },
          component: () => import("../views/AdminY/ProductDetail.vue"), // 商品详情
          props: true, // 启用 props 接收 id 参数
        },
      ],
    },

    {
      path: "/login",
      name: "Login",
      meta: { title: "登录", requiresAuth: false },
      component: () => import("../views/Login.vue"),
    },
    {
      path: "/register",
      name: "Register",
      meta: { title: "注册", requiresAuth: false },
      component: () => import("../views/Register.vue"),
    },
    {
      path: "/404",
      name: "NotFound",
      meta: { title: "404找不到页面", requiresAuth: false },
      component: () => import("../views/404.vue"),
    },
    { path: "/:pathMatch(.*)", redirect: "/404" }, // 通配符路由放在最后，用于匹配未定义的路径
  ],
});

router.beforeEach((to, from, next) => {
  document.title = to.meta.title;
  // 获取用户角色（从 localStorage 中获取）
  const userRole =
    localStorage.getItem("userRole") ||
    (JSON.parse(localStorage.getItem("xm-pro-user")) || {}).role;
  console.log(`用户角色：${userRole}`);
  // 目标路由需要认证
  if (to.meta.requiresAuth) {
    if (!userRole) {
      return next({ path: "/login" });
    }
    if (to.meta.role && to.meta.role !== userRole) {
      return next({ path: "/404" });
    }
    return next();
  }
  console.log("无需认证，直接访问");
  next();
});

export default router;
